import './public/index'
import server from './date/url'
import './public/toastr'

let $valid     = $('#getValidCode'),
    $submit    = $('#identifyTest_btn'),
    $validCode = $('#validCode'),
    timeIndex  = 120,
    timer,
    app

app = new Vue({
  el: '#box',
  data: function() {
    return {
      no: '',
      queryTime: '',
      reportTime: '',
      name: '',
      certificateType: '',
      certificateNo: '',
      marry: '',
      outline: {
        // 信用卡
        creditCard: {
          // 账户数
          count: 0,
          // 未结清/未销户账户数
          uncleared: 0,
          // 发生过逾期的账户数
          overdue: 0,
          // 发生过90天以上逾期的账户数
          overdue90: 0,
          // 为他人担保笔数
          guarantee: 0
        },
        // 购房贷款
        house: {
          // 账户数
          count: 0,
          // 未结清/未销户账户数
          uncleared: 0,
          // 发生过逾期的账户数
          overdue: 0,
          // 发生过90天以上逾期的账户数
          overdue90: 0,
          // 为他人担保笔数
          guarantee: 0
        },
        // 购房贷款
        other: {
          // 账户数
          count: 0,
          // 未结清/未销户账户数
          uncleared: 0,
          // 发生过逾期的账户数
          overdue: 0,
          // 发生过90天以上逾期的账户数
          overdue90: 0,
          // 为他人担保笔数
          guarantee: 0
        },
      },
      creditCard: [],
      other: [],
      public: [],
      query: {
        organ: [],
        own: []
      }
    }
  },
  mounted: function() {
    $('.c-tab-tit .c-tab-icon').on('click',(e) => {
      
      let $el  = $(e.target),
          rank = $el.index()

      $el.addClass('active').siblings().removeClass('active')

      $el.parents('.c-tab-tit').siblings('.c-tab-content').children().eq(rank).show().siblings().hide()
    })
  }
})

$valid.on('click', function(e) {
  e.preventDefault()

  if ($(this).hasClass('disabled')) {
    return
  }
  
  let loading = layer.msg('获取验证码中', {
                  icon: 16,
                  shade: .6,
                  time: 0
                })
  
  $.ajax({
    url:server,
    type: 'post',
    data: {
      task: 'getReportValidCode',
      stamp: new Date().getTime()
    }
  })
  .done(function(res) {
    console.log("success")
    console.log(res)

    if (res.status === 'true' || res.status === true) {
      layer.msg('验证码发送成功')

      $valid
        .attr('disabled', true)
        .addClass('disabled')
        .text(timeIndex)

      timer = setInterval(function() {
        
        if (timeIndex <= 0) {
          clearInterval(timer)

          $valid
            .removeAttr('disabled')
            .removeClass('disabled')
            .text('重新获取验证码')
        }

        timeIndex -= 1

        $valid.text(timeIndex)

      }, 1000)

    } else {
      toastr.error(res.message || '')
    }

  })
  .fail(function(res) {
    console.log("error")
    console.log(res)
  })
  .always(function() {
    // console.log("complete")
    layer.close(loading)
  })
})

$submit.on('click', function(e) {
  e.preventDefault()
  
  let validCode = $.trim($validCode.val())

  if (!validCode.length) {
    layer.msg('请输入验证码')

    return
  }

  let loading = layer.msg('获取验证码中', {
                  icon: 16,
                  shade: .6,
                  time: 0
                })
  
  $.ajax({
    url:server,
    type: 'post',
    data: {
      task: 'inputReportForm',
      stamp: new Date().getTime(),
      data: {
        valid: validCode
      }
    }
  })
  .done(function(res) {
    console.log("success")
    console.log(res)

    if (res.status === 'true' || res.status === true) {
      console.log('yes')

      $('#queryPage').removeClass('page-current')
      $('#reportPage').addClass('page-current')

      $.extend(app.$data, res.data)

    } else {
      toastr.error(res.message || '')
    }

  })
  .fail(function(res) {
    console.log("error")
    console.log(res)
  })
  .always(function() {
    // console.log("complete")
    layer.close(loading)
  })

  // window.location.href = './creditReport.html'

})